package de.ama.view;

import de.ama.crawler.Entry;
import de.ama.util.Util;

import javax.swing.table.AbstractTableModel;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: ama
 * Date: 15.02.11
 * Time: 10:51
 * To change this template use File | Settings | File Templates.
 */
public class EntryTableModel extends AbstractTableModel {
    private int columns = 6;
    List<Entry> entries = null;
    List<Entry> initial_entries = null;
    List<Integer> filter;
    String filterTag = null;
    String inverseFilterTag = null;
    String sortKey = null;

    public EntryTableModel(List<Entry> entries, int columns) {
        this.columns = columns;
        this.entries = new ArrayList<Entry>(entries);
        this.initial_entries = entries;
    }

    public int getRowCount() {
        return (entries.size() / columns) + 1;
    }

    public int getColumnCount() {
        return columns;
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {
        return Entry.class;
    }

    public Object getValueAt(int rowIndex, int columnIndex) {
        int index = rowIndex * columns + columnIndex;
        if (index >= entries.size()) {
            return null;
        }
        return entries.get(index);
    }

    public List<Entry> getEntrys() {
        return entries;
    }

    public boolean replaceEntry(Entry old, Entry replace) {
        int i = entries.indexOf(old);
        if (i >= 0) {
            entries.set(i, replace);
            filterAndSort();
            return true;
        }
        return false;
    }

    public boolean removeEntry(Entry entry) {
        if (entries.remove(entry)) {
            filterAndSort();
            return true;
        }
        return false;
    }

    public boolean addEntry(Entry entry) {
        if (!replaceEntry(entry, entry)) {
            entries.add(entry);
        }
        filterAndSort();
        return true;
    }

    public void filterAndSort() {
        entries.clear();
        filter();
        sort();
        fireTableDataChanged();
    }

    private void sort() {
        Collections.sort(entries, new Comparator<Entry>() {
            @Override
            public int compare(Entry e1, Entry e2) {
                return new Long(e2.getTime()).compareTo(e1.getTime());
            }
        });
    }

    private void filter() {

        if (filter == null && Util.isEmpty(filterTag)&& Util.isEmpty(inverseFilterTag)) {
            entries.addAll(initial_entries);
            return;
        }

        for (Entry e : initial_entries) {
            if (!Util.isEmpty(inverseFilterTag) && e.hasTag(inverseFilterTag)) {
                continue;
            }

            if (!Util.isEmpty(filterTag) && !e.hasTag(filterTag)) {
                continue;
            }

            if (filter != null && !filter.contains(e.getId())) {
                continue;
            }

            entries.add(e);
        }
    }

    public void setInverseFilterTag(String inverseFilterTag) {
        if("alle".equalsIgnoreCase(inverseFilterTag)){
            this.inverseFilterTag = null;
            return;
        }
        this.inverseFilterTag = inverseFilterTag;
    }

    public void setFilterTag(String filterTag) {
        if("alle".equalsIgnoreCase(filterTag)){
            this.filterTag = null;
            return;
        }
        this.filterTag = filterTag;
    }

    public void setFilter(List<Integer> filter) {
        this.filter = filter;
    }

    public void setSortKey(String sortKey) {
        this.sortKey = sortKey;
    }

    public void setColumns(int columns) {
        if (this.columns != columns) {
            this.columns = columns;
            fireTableStructureChanged();
        }
    }
}
